import time

import ddddocr
import pyautogui
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

# 堡垒机外部
BASTION_HOST_URL = 'https://172.16.20.73/fort/pages/login.jsp'
BASTION_HOST_LOGIN_NAME = '黎梓莹'
BASTION_HOST_PASSWORD = 'b2bwingscc$'

# 堡垒机内部
BASTION_HOST_ACCOUNT_NAME = 'lzy'
BASTION_HOST_PASS = 'w&lIsKJdRh3E'

# 验证码保存位置
CAPTCHA_CODE_URL = 'D:\PycharmProjects\pythonProject\captcha_code.png'

# vpn的x,y坐标位置
VPN_OPEN_X, VPN_OPEN_Y = 1710, 30
# vpn打开后 "继续" 的x,y坐标位置
VPN_CONTINUE_X, VPN_CONTINUE_Y = 1040, 675
# vpn最后打开 的x,y坐标位置
VPN_END_X, VPN_END_Y = 550, 250

# 移动鼠标到vpn 双击鼠标左键打开
pyautogui.moveTo(VPN_OPEN_X, VPN_OPEN_Y)
pyautogui.doubleClick()

time.sleep(5)

# 单击鼠标左键继续
pyautogui.moveTo(VPN_CONTINUE_X, VPN_CONTINUE_Y)
pyautogui.click()

time.sleep(8)

options = Options()
options.add_experimental_option("detach", True)  # 关键：分离浏览器

service = Service(ChromeDriverManager().install())  # 修改为你的实际路径
driver = webdriver.Chrome(service=service, options=options)

# 先登录堡垒机
driver.get(BASTION_HOST_URL)

# 创建等待对象（推荐：等待按钮可点击）
wait = WebDriverWait(driver, 1)

# 点击高级按钮
advanced_button = wait.until(EC.element_to_be_clickable((By.ID, "details-button")))
advanced_button.click()

# 点击继续访问
proceed_link = wait.until(EC.element_to_be_clickable((By.ID, "proceed-link")))
proceed_link.click()


def logon():
    while True:
        try:
            login_name = wait.until(EC.element_to_be_clickable((By.ID, "loginName")))
            login_name.clear()
            login_name.send_keys(BASTION_HOST_LOGIN_NAME)

            password = wait.until(EC.element_to_be_clickable((By.ID, "password")))
            password.clear()
            password.send_keys(BASTION_HOST_PASSWORD)

            captcha_code = wait.until(EC.element_to_be_clickable((By.ID, "code_ids")))
            # 验证码元素截图
            captcha_code.screenshot(CAPTCHA_CODE_URL)
        except BaseException:
            print('页面加载中...')

        try:
            with open(CAPTCHA_CODE_URL, 'rb') as f:
                image = f.read()
                # 进行OCR识别
                ocr = ddddocr.DdddOcr()
                result = ocr.classification(image)
                print('识别到的验证码是：' + result)
        except BaseException:
            raise Exception("验证码识别异常！")

        try:
            code_input = wait.until(EC.element_to_be_clickable((By.ID, "test")))
            code_input.clear()
            code_input.send_keys(result)

            sum_button = wait.until(EC.element_to_be_clickable((By.ID, "sum_button")))
            sum_button.click()
        except BaseException:
            print('页面加载中......')

        try:
            sure = wait.until(EC.element_to_be_clickable((By.ID, "layOut_yesB")))
            sure.click()
        except BaseException:
            print('无需点击，直接跳过')

        time.sleep(1)
        try:
            driver.switch_to.frame('mainframe')
            break
        except BaseException:
            time.sleep(0.5)


logon()

computer = wait.until(EC.element_to_be_clickable((By.XPATH, '//img[@src="/fort/images/u180.png"]')))
computer.click()

account_name = wait.until(EC.element_to_be_clickable((By.ID, "account_name")))
account_name.clear()
account_name.send_keys(BASTION_HOST_ACCOUNT_NAME)

account_pass = wait.until(EC.element_to_be_clickable((By.ID, "account_pass")))
account_pass.clear()
account_pass.send_keys(BASTION_HOST_PASS)

confirm = wait.until(EC.element_to_be_clickable((By.XPATH, '//button[@onclick="dialog_addAccountClick();"]')))
confirm.click()

time.sleep(1)
pyautogui.moveTo(VPN_END_X, VPN_END_Y)
pyautogui.doubleClick()
